package org.jeecg.modules.experiment.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.course.entity.AstuCourse;
import org.jeecg.modules.course.service.IAstuCourseService;
import org.jeecg.modules.experiment.entity.AstuExperiment;
import org.jeecg.modules.experiment.service.IAstuExperimentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 实验表
 * @Author: jeecg-boot
 * @Date:   2020-11-05
 * @Version: V1.0
 */
@Api(tags="实验表")
@RestController
@RequestMapping("/experiment/astuExperiment")
@Slf4j
public class AstuExperimentController extends JeecgController<AstuExperiment, IAstuExperimentService> {
	@Autowired
	private IAstuExperimentService astuExperimentService;
	@Autowired
	private IAstuCourseService astuCourseService;

	/**
	 * 分页列表查询
	 *
	 * @param astuExperiment
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "实验表-分页列表查询")
	@ApiOperation(value="实验表-分页列表查询", notes="实验表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(AstuExperiment astuExperiment,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<AstuExperiment> queryWrapper = QueryGenerator.initQueryWrapper(astuExperiment, req.getParameterMap());
		Page<AstuExperiment> page = new Page<AstuExperiment>(pageNo, pageSize);
		IPage<AstuExperiment> pageList = astuExperimentService.page(page, queryWrapper);
		List<AstuExperiment> records=new ArrayList<>();
		for (AstuExperiment record : pageList.getRecords()) {
			if(record.getCourseId() == null){
				continue;
			}else{
				AstuCourse astuCourse = astuCourseService.getBaseMapper().selectById(record.getCourseId());
				record.setCourseId(astuCourse.getCourseName());
				if (records != null) {
					records.add(record);
				}
			}
		}
		pageList.setRecords(records);
		return Result.OK(pageList);
	}

	/**
	 *   提供课程信息
	 * @param
	 * @return
	 */
	@AutoLog(value = "课程表-信息")
	@ApiOperation(value="课程表-信息", notes="课程表-信息")
	@GetMapping(value = "/courseInfo")
	public List<AstuCourse> couserInfo(){
		return  astuExperimentService.selectCourseNameAndTeacherNumberAndTeacherNumber();
	}
	/**
	 *   添加
	 *
	 * @param astuExperiment
	 * @return
	 */
	@AutoLog(value = "实验表-添加")
	@ApiOperation(value="实验表-添加", notes="实验表-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody AstuExperiment astuExperiment) {
		astuExperimentService.save(astuExperiment);
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param astuExperiment
	 * @return
	 */
	@AutoLog(value = "实验表-编辑")
	@ApiOperation(value="实验表-编辑", notes="实验表-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody AstuExperiment astuExperiment) {
		astuExperimentService.updateById(astuExperiment);
		return Result.OK("编辑成功!");

	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "实验表-通过id删除")
	@ApiOperation(value="实验表-通过id删除", notes="实验表-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		astuExperimentService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "实验表-批量删除")
	@ApiOperation(value="实验表-批量删除", notes="实验表-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.astuExperimentService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "实验表-通过id查询")
	@ApiOperation(value="实验表-通过id查询", notes="实验表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		AstuExperiment astuExperiment = astuExperimentService.getById(id);
		if(astuExperiment==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(astuExperiment);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param astuExperiment
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, AstuExperiment astuExperiment) {
        return super.exportXls(request, astuExperiment, AstuExperiment.class, "实验表");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, AstuExperiment.class);
    }

}
